/*
 * Copyright(c) 2020-2100 万达信息股份有限公司 版权所有
 * WONDERS INFORMATION CO., LTD. ALL RIGHTS RESERVED.
 */

package com.wondersgroup.sqlengine.script;

import com.wondersgroup.sqlengine.Context;
import com.wondersgroup.sqlengine.token.GenericTokenParser;
import com.wondersgroup.sqlengine.token.TokenHandler;

/**
 * 文本片段
 *
 * @author: huym
 * @date: 2021/1/22
 */
public class TextFragment implements SqlFragment {

	private String sql;

	public TextFragment(String sql) {
		this.sql = sql;
	}

	public boolean apply(final Context context) {
		GenericTokenParser genericTokenParser = new GenericTokenParser("${", "}", (c) -> {
			Object value = OgnlCache.getValue(c, context.getBinding());
			return value == null ? "" : value.toString();
		});
		context.appendSql(genericTokenParser.parse(sql));
		return true;
	}

}